var mods = [
	'element', 'sidebar', 'mockjs', 'select',
	'tabs', 'menu', 'route', 'utils', 'component', 'kit'
];

layui.define(mods, function(exports) {
	var element = layui.element,
		utils = layui.utils,
		$ = layui.jquery,
		_ = layui.lodash,
		route = layui.route,
		tabs = layui.tabs,
		layer = layui.layer,
		menu = layui.menu,
		component = layui.component,
		kit = layui.kit;

	var Admin = function() {
		this.config = {
			elem: '#app',
			loadType: 'SPA'
		};
		this.version = '1.0.0';
	};

	Admin.prototype.ready = function(callback) {
		var that = this,
			config = that.config;

		// 初始化加载方式
		var getItem = utils.localStorage.getItem;
		var setting = getItem("KITADMIN_SETTING_LOADTYPE");
		if (setting !== null && setting.loadType !== undefined) {
			config.loadType = setting.loadType;
		}

		kit.set({
			type: config.loadType
		}).init();

		// 初始化路由
		_private.routeInit(config);
		// 初始化选项卡
		if (config.loadType === 'TABS') {
			_private.tabsInit();
		}
		// 初始化左侧菜单   -- 请先初始化选项卡再初始化菜单
		_private.menuInit(config);
		// 跳转至首页
		if (location.hash === '') {
			utils.setUrlState('主页', '#/');
		}

		// 处理 sidebar
		layui.sidebar.render({
			elem: '#ccleft',
			//content:'', 
			title: '这是左侧打开的栗子',
			shade: true,
			// shadeClose:false,
			direction: 'left',
			dynamicRender: true,
			url: 'views/table/teble2.html',
			width: '50%', //可以设置百分比和px
		});

		$('#cc').on('click', function() {
			var that = this;
			layui.sidebar.render({
				elem: that,
				//content:'', 
				title: '这是表单盒子',
				shade: true,
				// shadeClose:false,
				// direction: 'left'
				dynamicRender: true,
				url: 'views/form/index.html',
				width: '50%', //可以设置百分比和px
			});
		});
		// 监听头部右侧 nav
		component.on('nav(header_right)', function(_that) {
			var target = _that.elem.attr('kit-target');
			if (target === 'setting') {
				// 绑定sidebar
				layui.sidebar.render({
					elem: _that.elem,
					//content:'', 
					title: '设置',
					shade: true,
					// shadeClose:false,
					// direction: 'left'
					dynamicRender: true,
					url: 'views/setting.html',
					// width: '50%', //可以设置百分比和px
				});
			}
			if (target === 'help') {

			}
		});

		// 注入mock
		layui.mockjs.inject(APIs);

		// 初始化渲染
		if (config.loadType === 'SPA') {
			route.render();
		}

		// 执行回调函数
		typeof callback === 'function' && callback();
	}

	var _private = {
		routeInit: function(config) {
			var that = this;
			// route.set({
			//   beforeRender: function (route) {
			//     if (!utils.oneOf(route.path, ['/user/table', '/user/table2', '/'])) {
			//       return {
			//         id: new Date().getTime(),
			//         name: 'Unauthorized',
			//         path: '/exception/403',
			//         component: 'views/exception/403.html'
			//       };
			//     }
			//     return route;
			//   }
			// });
			// 配置路由
			var routeOpts = {
				name: 'hhadmin',
				routes: [
					//自定义添加
					{
						path: '/',
						component: 'views/video/list.html',
						name: '类型列表'

					}, {
						path: '/videotype/index',
						component: 'views/videotype/index.html',
						name: '类型列表'

					}, {
						path: '/teacher/list',
						component: 'views/teacher/list.html',
						name: '讲师列表'

					}, {
						path: '/teacher/add',
						component: 'views/teacher/one.html',
						name: '添加讲师'
					},
					{
						path: '/teacher/homepage',
						component: 'views/teacher/homepage.html',
						name: '讲师主页'

					}, {
						path: '/video/list',
						component: 'views/video/list.html',
						name: '视频列表'

					}, {
						path: '/video/homepage',
						component: 'views/video/homepage.html',
						name: '视频主页'

					}, {
						path: '/video/upvideo',
						component: 'views/video/upvideo.html',
						name: '视频详情'

					}, {
						path: '/setup/richtext',
						component: 'views/setup/richtext.html',
						name: '通用信息'

					}, {
						path: '/setup/list',
						component: 'views/setup/list.html',
						name: '网站设置'

					}, {
						path: '/banner/bannerup',
						component: 'views/banner/bannerup.html',
						name: '网站信息'

					}, {
						path: '/user/list',
						component: 'views/user/list.html',
						name: '用户'

					}, {
						path: '/user/one',
						component: 'views/user/one.html',
						name: '用户管理'

					}, {
						path: '/xxcourse/list',
						component: 'views/xxcourse/list.html',
						name: '线下课程'

					}, {
						path: '/xxcourse/det',
						component: 'views/xxcourse/det.html',
						name: '线下课程'

					}, {
						path: '/xxcourse/add',
						component: 'views/xxcourse/det.html',
						name: '新增线下课程'

					}, {
						path: '/xxcourse/homepage',
						component: 'views/xxcourse/homepage.html',
						name: '线下课主页'

					},
					{
						path: '/video/msjladd',
						component: 'views/video/msjladd.html',
						name: '名师交流'

					},
					{
						path: '/teachersexchange/det',
						component: 'views/teachersexchange/det.html',
						name: '名师交流'

					},
					{
						path: '/teachersexchange/list',
						component: 'views/teachersexchange/list.html',
						name: '名师交流列表'

					},
					{
						path: '/teachersexchange/one',
						component: 'views/teachersexchange/one.html',
						name: '名师交流'

					}, {
						path: '/source/index',
						component: 'views/source/index.html',
						name: '名师交流'

					},
					// 问卷调查
					{
						path: '/questionnaire/list',
						component: 'views/questionnaire/list.html',
						name: '问卷调查列表'

					},
					{
						path: '/questionnaire/add',
						component: 'views/questionnaire/det.html',
						name: '问卷调查新增'

					},
					{
						path: '/questionnaire/modify',
						component: 'views/questionnaire/det.html',
						name: '问卷调查修改'

					},
					{
						path: '/questionnaire/homepage',
						component: 'views/questionnaire/homepage.html',
						name: '问卷调查'

					},
					//案例答疑
					{
						path: '/case/homepage',
						component: 'views/case/homepage.html',
						name: '案例答疑主页'
					},
					{
						path: '/case/det',
						component: 'views/case/det.html',
						name: '案例答疑修改'
					},
					{
						path: '/case/add',
						component: 'views/case/det.html',
						name: '案例答疑新增'
					},
					{
						path: '/case/list',
						component: 'views/case/list.html',
						name: '案例答疑列表'
					},
					//测试
					{
						path: '/test/homepage',
						component: 'views/test/homepage.html',
						name: '测试主页'
					},
					{
						path: '/test/det',
						component: 'views/test/det.html',
						name: '测试修改'
					},
					{
						path: '/test/add',
						component: 'views/test/det.html',
						name: '测试新增'
					},
					{
						path: '/test/list',
						component: 'views/test/list.html',
						name: '测试列表'
					},
					// 统计中心
					{
						path: '/Statistics/source',
						component: 'views/statistics/source.html',
						name: '来源分析'
					}, {
						path: '/Statistics/test',
						component: 'views/statistics/test.html',
						name: '测试分析'
					}, {
						path: '/Statistics/content',
						component: 'views/statistics/content.html',
						name: '内容分析'
					}, {
						path: '/Statistics/user',
						component: 'views/statistics/user.html',
						name: '用户分析'
					},
					//运营中心
					//轮播导航
					{
						path: '/banner/homepage',
						component: 'views/banner/homepage.html',
						name: '轮播主页'
					},
					{
						path: '/banner/det',
						component: 'views/banner/det.html',
						name: '轮播修改'
					},
					{
						path: '/banner/add',
						component: 'views/banner/det.html',
						name: '轮播新增'
					},
					{
						path: '/banner/list',
						component: 'views/banner/list.html',
						name: '轮播列表'
					}, {
						path: '/remind/homepage',
						component: 'views/remind/homepage.html',
						name: '提醒主页'
					},
					{
						path: '/remind/det',
						component: 'views/remind/det.html',
						name: '提醒修改'
					},
					{
						path: '/remind/add',
						component: 'views/remind/det.html',
						name: '提醒新增'
					},
					{
						path: '/remind/list',
						component: 'views/remind/list.html',
						name: '提醒列表'
					}, {
						path: '/clause/homepage',
						component: 'views/clause/homepage.html',
						name: '条款主页'
					},
					{
						path: '/clause/det',
						component: 'views/clause/det.html',
						name: '条款修改'
					},
					{
						path: '/clause/add',
						component: 'views/clause/det.html',
						name: '条款新增'
					},
					{
						path: '/clause/list',
						component: 'views/clause/list.html',
						name: '条款列表'
					}, {
						path: '/user/homepage',
						component: 'views/user/one.html',
						name: '用户主页'
					},
					{
						path: '/user/det',
						component: 'views/user/edit.html',
						name: '用户修改'
					},
					{
						path: '/user/add',
						component: 'views/user/edit.html',
						name: '用户新增'
					},
					{
						path: '/user/list',
						component: 'views/user/list.html',
						name: '用户列表'
					}, {
						path: '/feedback/homepage',
						component: 'views/feedback/homepage.html',
						name: '用户反馈主页'
					},
					{
						path: '/feedback/det',
						component: 'views/feedback/det.html',
						name: '用户反馈修改'
					},
					{
						path: '/feedback/add',
						component: 'views/feedback/det.html',
						name: '用户反馈新增'
					},
					{
						path: '/feedback/list',
						component: 'views/feedback/list.html',
						name: '用户反馈列表'
					}, 	{
						path: '/account/my',
						component: 'views/account/my.html',
						name: '我的账号'
					},  {
						path: '/account/homepage',
						component: 'views/account/homepage.html',
						name: '子账号主页'
					},
					{
						path: '/account/det',
						component: 'views/account/det.html',
						name: '子账号修改'
					},
					{
						path: '/account/add',
						component: 'views/account/det.html',
						name: '子账号新增'
					},
					{
						path: '/account/list',
						component: 'views/account/list.html',
						name: '子账号列表'
					}, 
					
					
					{
						path: '/exception/403',
						component: 'views/exception/403.html',
						name: '403'
					}, {
						path: '/exception/404',
						component: 'views/exception/404.html',
						name: '404'
					}, {
						path: '/exception/500',
						component: 'views/exception/500.html',
						name: '500'
					},
					{
						path: '/upload/test',
						component: 'views/exception/500.html',
						name: '500'
					},

				]
			};
			if (config.loadType === 'TABS') {
				routeOpts.onChanged = function() {
					// 如果当前hash不存在选项卡列表中

					if (!tabs.existsByPath(location.hash)) {
						// 新增一个选项卡
						that.addTab(location.hash, new Date().getTime());

					} else {
						// 切换到已存在的选项卡
						tabs.switchByPath(location.hash);
					}
				}
			}
			// 设置路由
			route.setRoutes(routeOpts);
			return this;
		},
		addTab: function(href, layid) {
			var r = route.getRoute(href);
			if (r) {
				tabs.add({
					id: layid,
					title: r.name,
					path: href,
					component: r.component,
					rendered: false,
					icon: '&#xe62e;'
				});

			}
		},
		menuInit: function(config) {
			var that = this;
			// 配置menu
			menu.set({
				dynamicRender: true,
				isJump: config.loadType === 'SPA',
				cacheKey: 'hhmenu',
				onClicked: function(obj) {
					console.log(obj)
					$("#indextitle").html($(".layui-this span").html())
					if (config.loadType === 'TABS') {
						if (!obj.hasChild) {
							var data = obj.data;
							var href = data.href;
							var layid = data.layid;
							that.addTab(href, layid);

						} else {

						}
					}
				},
				elem: '#menu-box',
				remote: {
					url: '/api/getmenus',
					method: 'post'
				},
				cached: false
			}).render();
			return that;
		},
		tabsInit: function() {
			tabs.set({
				onChanged: function(layid) {

					if (tab !== null) {
						utils.setUrlState(tab.title, tab.path);
					}
				}
			}).render(function(obj) {
				// 如果只有首页的选项卡
				if (obj.isIndex) {
					route.render('#/');
				}
			});
		}
	}

	var admin = new Admin();
	admin.ready(function() {
		console.log('Init successed.');
	});

	//输出admin接口
	exports('admin', {});
});
